import { Effect, Reducer } from 'umi';

import { getRouters } from '@/services/user';


// export interface RoutersModelState {
//     routers: Array<any>;
// }

export interface RoutersModelType {
    namespace: 'routers';
    state: Array<any>;
    effects: {
        getRouters: Effect;
    };
    reducers: {
        setRoutes: Reducer
    };
}

function routersFn(data:Array<any>,path:string = '') {
    return data.map(m => {
        m.path = path + m.path
        if(m.children) {
            routersFn(m.children, m.path + '/')
        }
        return m;
    })
}


const RoutersModel: RoutersModelType = {
    namespace: 'routers',

    state: [],

    effects: {
        *getRouters(_, { call, put }) {
            const response = yield call(getRouters);
            if (response.code == 200) {
                // console.log(routersFn(response.data))
                yield put({
                    type: 'setRoutes',
                    payload: response.data
                })
            }
        }
    },

    reducers: {
        setRoutes(state, { payload }) {
            return payload
        }
    },
};

export default RoutersModel;
